package com.jinyi.wwdeng.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.DialogTipsBinding
import com.jinyi.wwdeng.ui.permission.PermissionConfig
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 提示dialog
 */
class TipsDialog: ProDialog {
    private lateinit var binding: DialogTipsBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogTipsBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.ivClose) //关闭
        setOnClickListener(R.id.btnOne) //单个按钮
        setOnClickListener(R.id.btnLeft) //两个按钮 左侧按钮
        setOnClickListener(R.id.btnRight) //两个按钮 又侧按钮
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 设置提示图标
     */
    fun setTipsIconResource(resId: Int){
        binding.ivIcon.setImageResource(resId)
    }

    /**
     * 设置提示title
     */
    fun setTipsTitle(title: String){
        binding.tvTipsTitle.text = title
    }

    /**
     * 设置提示语
     */
    fun setTips(tips: String){
        binding.tvTips.text = tips
    }

    /**
     * 设置显示一个按钮或两个按钮
     */
    fun isDoubleButton(isDouble: Boolean){
        if (isDouble){//显示两个图标
            binding.btnOne.visibility = View.GONE
            binding.doubleLayout.visibility = View.VISIBLE
        } else {
            binding.btnOne.visibility = View.VISIBLE
            binding.doubleLayout.visibility = View.GONE
        }
    }

    /**
     * 设置单个按钮文字
     */
    fun setButtonOneText(text: String){
        binding.btnOne.text = text
        isDoubleButton(false)
    }

    /**
     * 设置单个按钮背景
     */
    fun setButtonOneBackgroundResource(resId: Int){
        binding.btnOne.setBackgroundResource(resId)
        isDoubleButton(false)
    }

    /**
     * 设置两个按钮左侧按钮文字
     */
    fun setButtonLeftText(text: String){
        binding.btnLeft.text = text
        isDoubleButton(true)
    }

    /**
     * 设置两个按钮左侧按钮背景
     */
    fun setButtonLeftBackgroundResource(resId: Int){
        binding.btnLeft.setBackgroundResource(resId)
        isDoubleButton(true)
    }

    /**
     * 设置两个按钮右侧侧按钮文字
     */
    fun setButtonRightText(text: String){
        binding.btnRight.text = text
        isDoubleButton(true)
    }

    /**
     * 设置两个按钮右侧按钮背景
     */
    fun setButtonRightBackgroundResource(resId: Int){
        binding.btnRight.setBackgroundResource(resId)
        isDoubleButton(true)
    }

    /**
     * 设置两个按钮文字
     */
    fun setButtonDoubleText(leftText: String,rightText: String){
        binding.btnLeft.text = leftText
        binding.btnRight.text = rightText
        isDoubleButton(true)
    }

    /**
     * 设置两个按钮背景
     */
    fun setButtonDoubleBackgroundResource(leftResId: Int,rightResId: Int){
        binding.btnLeft.setBackgroundResource(leftResId)
        binding.btnRight.setBackgroundResource(rightResId)
        isDoubleButton(true)
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        TrackEventUtils.logEvent(TrackEventType.DIALOG.DIALOG_KIND_TIPS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        TrackEventUtils.logEvent(TrackEventType.DIALOG.DIALOG_KIND_TIPS_DISMISS,eventRemark)
    }
}